package com.heu.blood.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.match.service.BloodInformationService;
import com.heu.blood.recycle.dao.BloodBagRecycleDao;
import com.heu.blood.recycle.entity.BloodBagRecycleEntity;
import com.heu.blood.recycle.service.BloodBagRecycleService;
import com.heu.blood.security.entity.LoginUser;
import com.heu.blood.storge.service.BloodInStorgeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("bloodBagRecycleService")
public class BloodBagRecycleServiceImpl extends ServiceImpl<BloodBagRecycleDao, BloodBagRecycleEntity> implements BloodBagRecycleService {


    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private BloodInStorgeService bloodInStorgeService;
    @Autowired
    private BloodBagRecycleDao bloodBagRecycleDao;



    /**
     * 根据BloodId查找血袋回收信息
     * @param bloodId
     * @return
     */
    @Override
    public BloodBagRecycleEntity query(String bloodId) {
        LambdaQueryWrapper<BloodBagRecycleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BloodBagRecycleEntity::getBloodId,bloodId)
                    .eq(BloodBagRecycleEntity::getIfShow,1);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        BloodBagRecycleEntity entity = new BloodBagRecycleEntity();
        if(Strings.isNotBlank((String)params.get("bloodId")))
            entity.setBloodId((String)params.get("bloodId"));
        if(Strings.isNotBlank((String)params.get("number")))
            entity.setNumber((String)params.get("number"));
        if(Strings.isNotBlank((String)params.get("recyclePeople")))
            entity.setRecyclePeople((String)params.get("recyclePeople"));
        if(Strings.isNotBlank((String)params.get("recyclePeopleNumber")))
            entity.setRecyclePeople((String)params.get("recyclePeopleNumber"));
        //添加时间范围条件
        String startTime = null;
        String endTime = null;
        if(Strings.isNotBlank((String)params.get("startTime")))
            startTime = (String)params.get("startTime");
        if(Strings.isNotBlank((String)params.get("endTime")))
            endTime = (String)params.get("endTime");
        //构建构造器
        LambdaQueryWrapper<BloodBagRecycleEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(entity.getBloodId()), BloodBagRecycleEntity::getBloodId,entity.getBloodId())
                    .eq(Strings.isNotBlank(entity.getNumber()), BloodBagRecycleEntity::getNumber,entity.getNumber())
                    .eq(Strings.isNotBlank(entity.getRecyclePeople()), BloodBagRecycleEntity::getRecyclePeople,entity.getRecyclePeople())
                    .eq(Strings.isNotBlank(entity.getRecyclePeopleNumber()), BloodBagRecycleEntity::getRecyclePeopleNumber,entity.getRecyclePeopleNumber())
                    .ge(Strings.isNotBlank(startTime), BloodBagRecycleEntity::getRecycleTime,startTime)
                    .le(Strings.isNotBlank(endTime), BloodBagRecycleEntity::getRecycleTime,endTime)
                    .eq(BloodBagRecycleEntity::getIfShow,1);

        IPage<BloodBagRecycleEntity> page = this.page(
                new Query<BloodBagRecycleEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(BloodBagRecycleEntity bloodBagRecycle) {

        //获取当前线程认证信息
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null && authentication.isAuthenticated()){
            //获取当前用户id、name
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            //写入当前用户信息
            bloodBagRecycle.setRecyclePeople(loginUser.getUser().getUserName());
            bloodBagRecycle.setRecyclePeopleNumber(String.valueOf(loginUser.getUser().getUserId()));
        }

        int insert = bloodBagRecycleDao.insert(bloodBagRecycle);

        //更新血液信息表
        boolean informationgUpdate = bloodInformationService.updateBloodStateByBloodId(bloodBagRecycle.getBloodId(), "BLOOD_BAG_RECYCLE");
        //更新库存表
        boolean storgeUpdate = bloodInStorgeService.updateInStorgeBloodStatebyBloodId(bloodBagRecycle.getBloodId(),"BLOOD_BAG_RECYCLE");

        return informationgUpdate && storgeUpdate && insert == 1;
    }
}